const client = require("../../models/index.js")
const encryption = require("../../config/encryption.js")
const jwt = require("../../config/jwt.js"); 
const utils = require("../../utils/index.js"); 
const { password } = require("../../config/database.js");

class HomeController{
    // 登录帐号
    static login(request,response){  
        let {username,password} = request.body;
        client.query("select * from user_msg where username = ?",[username],function(err,results){ 
            let resBody = null;
            // 验证帐号是否存在
            if(!results || !results.length){
                resBody =  {msg:"登录失败！帐号不存在",data:null,code:403}
            }else{
                // 只取第一条匹配的数据
                let row = results[0]   
                // 验证密码是否正确
                if(!encryption.compareEncrypt(password,row.password)){
                    resBody =  {msg:"登录失败！密码错误",data:null,code:403}
                }else{
                    // 生成token
                    let token = jwt.createToken(row.id)
                    // 保存token，存放在数据库中
                    client.query("update user_msg set token = ? where id = ?",[token,row.id]) 
                    resBody =  {msg:"登录成功！",access_token:token,code:200}
                } 
            }
            response.send(resBody)
        })
    } 
    // 注册帐号
    static register(request,response){  
        let {username,password,sex,address} = request.body;
        client.query("select * from user_msg where username = ?",[username],function(err,results){ 
            let resBody = null;
            // 验证帐号是否存在(不允许同名的用户存在)
            console.log(results)
            if(results && results.length){
                resBody =  {msg:"注册失败！当前帐号名已经被注册",data:null,code:403}
            }else{  
                // 加密密码
                password = encryption.createEncrypt(password)
                // 添加新用户
                client.query("insert into user_msg(username,password,sex,address,isDelete,registerTime) values(?,?,?,?,?,?)",[username,password,sex,address,"0",utils.mysqlDateType(true)]) 
                resBody =  {msg:"注册成功！",data:null,code:200} 
            }
            response.send(resBody)
        })
    }
    // 退出登录
    static logout(request,response){
        let user_id = jwt.getHeaderToken(request)
        client.query("update user_msg set token=? where id = ?",["",user_id])
        response.send({msg:"退出登录成功",code:200})
    }
    // 注销帐号
    static unregister(request,response){ 
        let user_id = jwt.getHeaderToken(request) 
        client.query("update user_msg set token=?,isDelete=?,deleteTime=current_timestamp() where id = ?",["","1",user_id])
        response.send({msg:"注销帐号成功",code:200})
    }
 
    // 用户详情
    static about(request,response){ 
        let user_id = jwt.getHeaderToken(request) 
        client.query("select * from user_msg where id = ?",[user_id],function(err,results){
            let row = results ? results[0] : results
            response.send({msg:"获取用户信息成功",data:row,code:200})
        })
    } 
    // 修改信息 
    static updateInfo(request,response){ 
        let user_id = jwt.getHeaderToken(request) 
        let {sex,address,email,name} = request.body;
        client.query("update user_msg set sex=?,address=?,email=?,name=?,updateTime=current_timestamp() where id = ?",[sex,address,email,name,user_id])
        response.send({msg:"更新用户信息成功！",code:200})
    } 
    // 修改密码 
    static updatePwd(request,response){ 
        let user_id = jwt.getHeaderToken(request) 
        let {old_password,new_password}=request.body
        // 判断当前的密码是否正确
        client.query("select * from user_msg where id = ?",[user_id],function(err,results){
            let row = results ? results[0] : results
            if(!encryption.compareEncrypt(old_password,row.password)){ 
                response.send({msg:"更新失败！密码错误",code:403})
            }else{ 
                // 加密密码
                new_password = encryption.createEncrypt(new_password)
                client.query("update user_msg set password=?,updateTime=current_timestamp() where id = ?",[new_password,user_id])
                response.send({msg:"更新密码成功！",code:200})
            }
        })
    }

}

module.exports = HomeController;